package com.wrench.elevator.install.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;

import wrench.elevator.install.util.WrenchElevatorInstallException;
import wrench.elevator.install.util.WrenchElevatorInstallUtil;

import com.alibaba.fastjson.JSON;
import com.wrench.elevator.install.constant.Result;
import com.wrench.elevator.install.dao.BuildingDao;
import com.wrench.elevator.install.domain.Building;


@Component
public class BuildingService
{
    
    
    @Resource
    private BuildingDao dao ;
    
    private static final Logger logger = LoggerFactory.getLogger(BuildingService.class);
    
    
    /**
     * 获取小区列表
     */
    public List<Building> loadBuildingList(String user_id, String power)
       throws WrenchElevatorInstallException
    {
        if (WrenchElevatorInstallUtil.hasNull(user_id,power))
        {
            logger.error("==> param is not found , user_id = {} , power = {}  ",
                user_id,
                power
               );
            throw new WrenchElevatorInstallException(Result.PARAM_IS_NOT_FOUND_CODE, Result.PARAM_IS_NOT_FOUND_MESSAGE);
        }
        
        if (!WrenchElevatorInstallUtil.areInt(user_id))
        {
            logger.error("==> string is not int type , district_id = {} ", user_id);
            throw new WrenchElevatorInstallException(Result.STRING_IS_NOT_INT_TYPE_CODE, Result.STRING_IS_NOT_INT_TYPE_MESSAGE);
        }
        
        List<Building> buildingList = null;
        
        try
        {
           buildingList = dao.loadBuildingList(user_id,power);
        }
        catch (Exception e)
        {
            logger.error("==> dao.loadBuildingList(user_id,power) execute error , user_id = {} ,power = {} , error = {}  ",
                user_id,
                power,
                e);
            throw new WrenchElevatorInstallException(Result.DB_ERROR_CODE, Result.DB_ERROR_MESSAGE);
        }
        logger.debug("==> dao.loadBuildingList(user_id,power) execute success , user_id = {} ,power = {} ", 
            user_id,
            power);
        if (buildingList == null)
        {
            logger.debug("==> building is null ");
            throw new WrenchElevatorInstallException(Result.BUILDING_LIST_IS_NULL_CODE, Result.BUILDING_LIST_IS_NULL_MESSAGE);
        
        }
        return buildingList;
    }
}
